/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.utils;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Peter on 2016/7/26.
 */
public class ServerEventBusUtils {

    private static AsyncEventBus eventBus;

    private static EventBus getInstance() {
        if (eventBus == null) {
            final int maxPoolSize = 5;
            final long keepAliveTime = 3600L;
            final int capacity = 100;
            ExecutorService threadPool = new ThreadPoolExecutor(0, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(capacity));
            eventBus = new AsyncEventBus(threadPool);
        }
        return eventBus;
    }

    /**
     * Register Server Event to bus.
     *
     * @param object index.
     */
    public static void register(Object object) {
        getInstance().register(object);
    }

    /**
     * Post Event with Server Event.
     *
     * @param object target.
     */
    public static void post(Object object) {
        getInstance().post(object);
    }

}
